/**
 * 返回随机时间
 * @returns String
 */
function randomTime() {
    let randomYearMun = Math.floor(Math.random() * 2023); //随机生成0-2022的数
    let randomMonthNum = Math.floor(Math.random() * 11);  //随机生成0-11的数
    let randomDateNum = Math.ceil(Math.random() * 30);    //随机生成1-30的数
    let randomHourNum = Math.ceil(Math.random() * 24);    //随机生成1-24的数
    let randomMinuteNum = Math.ceil(Math.random() * 60);  //随机生成1-60的数
    let randomSecondNum = Math.ceil(Math.random() * 60);  //随机生成1-60的数
    return time()
        .year(randomYearMun)
        .month(randomMonthNum)
        .date(randomDateNum)
        .hour(randomHourNum)
        .minute(randomMinuteNum)
        .second(randomSecondNum)
        .format('Y-m-d H:i:s');
}

/**
 * 获取输入时间距今时间
 * @param {String} ('2022-5-10 12:00:00')
 * @returns String
 */
function dateFormat(time) {
    let nowTime = new Date();
    let nowTimes = nowTime.getTime();
    let oldTime = new Date(time);
    let oldTimes = oldTime.getTime();
    let diff = (nowTimes - oldTimes) / 1000;

    if (diff < 10) return '刚刚'
    if (diff < 60) return `${diff}秒前`
    if (diff < 60 * 60) return `${Math.floor(diff / 60)}分钟前`
    if (diff < 60 * 60 * 60) return `${Math.floor(diff / 60 / 60)}小时前`
    if (diff < 60 * 60 * 60 * 24) return `${Math.floor(diff / 60 / 60 / 24)}天前`
    if (diff < 60 * 60 * 60 * 24 * 30) return `${Math.floor(diff / 60 / 60 / 24 / 30)}月前`
    if (diff < 60 * 60 * 60 * 24 * 30 * 12) return `${Math.floor(diff / 60 / 60 / 24 / 30 / 12)}年前`

}

/**
  * 获取当前时间
  * @param {string} str 
  * @returns String
  */
function format(str) {
    let d = new Date();
    str = str.replaceAll('Y', d.getFullYear());
    str = str.replaceAll('m', zeroize(d.getMonth() + 1));
    str = str.replaceAll('d', zeroize(d.getDate()));
    str = str.replaceAll('H', zeroize(d.getHours()));
    str = str.replaceAll('i', zeroize(d.getMinutes()));
    str = str.replaceAll('s', zeroize(d.getSeconds()));
    return str;
}
/**
 * 字符串补0（传递进来的数字长度不够2位就补0
 * @param {String/Number} data
 * @returns  过滤后的数据
 */
function zeroize(data) {
    return String(data).length === 1 ? '0' + data : data;// 一位数时在前面补0
}

/**
 * 判断浏览器类型
 * @returns string
 */
function browserType() {
    let userAgent = navigator.userAgent; //取得浏览器的userAgent字符串
    let isOpera = userAgent.indexOf("Opera") > -1; //判断是否Opera浏览器
    let isIE = userAgent.indexOf("compatible") > -1 && userAgent.indexOf("MSIE") > -1 && !isOpera; //判断是否IE浏览器
    let isIE11 = userAgent.indexOf('Trident') > -1 && userAgent.indexOf("rv:11.0") > -1;
    let isEdge = userAgent.indexOf("Edge") > -1 && !isIE; //判断是否IE的Edge浏览器  
    let isFF = userAgent.indexOf("Firefox") > -1; //判断是否Firefox浏览器
    let isSafari = userAgent.indexOf("Safari") > -1 && userAgent.indexOf("Chrome") == -1; //判断是否Safari浏览器
    let isChrome = userAgent.indexOf("Chrome") > -1 && userAgent.indexOf("Safari") > -1; //判断Chrome浏览器

    if (isIE11) return 'IE11';
    if (isEdge) return "Edge";
    if (isFF) return "FF";
    if (isOpera) return "Opera";
    if (isSafari) return "Safari";
    if (isChrome) return "Chrome";
}

/**
 * 判断是PC还是移动端
 * @param {String} 
 * @returns {string}
*/
function PcandYd() {
    data = navigator.userAgent;
    if ((data.indexOf('Mac') || data.indexOf('Windows') || data.indexOf('Linux')) || data.indexOf('win') !== -1) {
        return '电脑端';
    }
    if ((data.indexOf('iPhone') || data.indexOf('iPad') || data.indexOf('Android') || data.indexOf('Mobile')) !== -1) {
        return '移动端';
    }
}

/**
 * 判断是否是微信浏览器
 *@param {String} 
 * @returns {Boolean}
 */
function is_weiXin() {
    let ua = navigator.userAgent.toLowerCase();
    if (ua.match(/MicroMessenger/i) == 'micromessenger') {
        return true;
    } else {
        return false;
    }
}

/**
 * 获取非行内样式
 * @param {object} 
 * @returns {str}
 */
function getStyle(obj, attr) {       // obj是对象,attr表示值
    if (obj.currentStyle) {           // 针对ie获取非行间样式
        return obj.currentStyle[attr];
    } else {                          // 非ie
        return getComputedStyle(obj, false)[attr];
    }
}

/**
 * 获取地址栏参数键对应的值
 * @param {string} key 
 * @returns string/false
 */
function getParams(key) {
    // 参数名=值&...&参数名=值
    // 1.获取?后的参数数据
    let query = window.location.search.substring(1);
    // 2. 转译成字符串
    query = decodeURI(query)
    // 3. 按&分割成数组
    let arr = query.split("&");
    for (let i = 0; i < arr.length; i++) {
        let temp = arr[i].split("=");
        if (temp[0] == key) return temp[1]
    }
    return false;
}
console.log(getParams('uname'))

/**
 * jq原理
 * @param {String} ele 元素
 */
function Jquery(ele) {
    let  eles = document.querySelectorAll(ele);
    for (let i = 0; i < eles.length; i++) {
        this[i] = eles[i]
    }
    this.length = eles.length
}

Jquery.prototype.css = function (cssName, cssValue) {
    for (let i = 0; i < this.length; i++) {
        this[i].style[cssName] = cssValue
    }
}

function $(ele) {
    return new Jquery(ele)
}